// pages/my/my.js
const app = getApp();
import {get,getLikeMenu} from "../../until/db"
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo:{},
    isLogin:false, //是否登录。 false 未登录  true，已经登录
    classIndex:0,
    class:["菜单","分类","关注"],
    recipes:[],
    types:[],
  lists:[],
  },
 async onLoad(){
    if(app.globalData.userInfo!=null){ 
      this.setData({
        userInfo:app.globalData.userInfo,
        isLogin:true
      })
    }
    else{
      app.getUserInfo=(res)=>{
        this.setData({
          userInfo:res.userInfo,
          isLogin:true
        })
      }
    }
    //获取我添加的菜单数据
    var openid = wx.getStorageSync("openid")
    // console.log(openid,"1234567");
    var result = await get("menu",{_openid:openid})

    //获取分类列表
    var result1 = await get("class")
    // console.log(result1);
    // console.log(result,"-----------1234567");
    // this.setData({
    //   recipes:result.data,
    //   types:result1.data
    // })

    var openid = wx.getStorageSync('openid');
    var result = await get("likes",{_openid:openid})
    // console.log(result);
    var arr = result.data.map(item=>{
      return item.menuId
    })
    //用muneId与menu里面的_id做对比，获取菜谱的信息
    var result = await getLikeMenu(arr)
    var lists = result.data;
    console.log(lists);
    
    lists.forEach(item=>{
      if(item.views==0){
        item.star=0
      }else if(item.views>0 && item.views<=10){
        item.star=1;
      }else if(item.views>10 && item.views<=20){
        item.star=2
      }else if(item.views>20 && item.views<=30){
        item.star=3
      }else if(item.views>30 && item.views<=40){
        item.star=4;
      }else{
        item.star=5
      }
    })
    this.setData({
      recipes:result.data,
      types:result1.data,
      lists
    })
  },

//用户登陆
  getInfo(e){
    // console.log(e);
    if(e.detail.userInfo !== undefined){
      this.setData({
        userInfo:e.detail.userInfo,
        isLogin:true
      })
    }
  },
  _delStyle(){
    wx.showModal({
      title:"删除提示",
      content:"确定要删除么？",
      
    })
  },
  //点击切换导航栏下面的内容
  onClick(e){
    // console.log(e);
    let classIndex = e.currentTarget.dataset.classindex
    // console.log(classIndex);
    this.setData({
      classIndex
    })
  },
  //发布菜谱的页面
  nav(){
    wx.navigateTo({
      url: "/pages/pbrecipe/pbrecipe",
    })
  },
  //进入详情页面
  toDetail(e){
    // console.log(e);
    var id = e.currentTarget.id
    // console.log(id);
    var name = e.currentTarget.dataset.name
    // console.log(id);
    wx.navigateTo({
      url: '/pages/detail/detail?id='+id+'&name='+name,
    })
  },
  //跳转到分类页面
  jump1(){
    wx.navigateTo({
      url: '/pages/category/category',
    })
  },
  //进入分类里面的详情
  toEnter(e){
    // console.log(e);
    var id = e.currentTarget.id
    var name = e.currentTarget.dataset.name
    wx.navigateTo({
      url: '/pages/list/list?id='+id+'&name='+name,
    })
  }

})